package edu.buffalo.son.garman.entity;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ClaimsRecordIterator implements RecordIterator {

    //public static final String CLAIMS_RECORDS_FILE_NAME = "ClaimsData.csv";
    public static final String CLAIMS_RECORDS_FILE_NAME = "dbo_CER_MEDICAID_FINAL_CLAIMS.txt";

    @Override
    public Iterator<ClaimsRecord> iterator() {

        try {
            Iterator<ClaimsRecord> _iter = new Iterator<ClaimsRecord>() {

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(
                                new FileInputStream(CLAIMS_RECORDS_FILE_NAME)));

                private ClaimsRecord nextRecord = getNextRecord();
                private ClaimsRecord currentRecord = null;

                @Override
                public boolean hasNext() {
                    if(nextRecord != null){
                        return true;
                    }
                    return false;
                }

                private ClaimsRecord getNextRecord() {
                    String line;
                    try {
                        line = reader.readLine();
                        if(line == null || line.trim().equals("")) {
                            return null;
                        }
                        String[] fields = line.split("\\|");
                        ClaimType claimType = ClaimType.valueOf(fields[5].trim());
                        // personID, DOS, claimType, dx1 - dx5
                        return new ClaimsRecord(fields[0], fields[6], claimType, 
                                fields[10], fields[11], fields[12], fields[13], fields[14]);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    return null;
                }

                @Override
                public ClaimsRecord next() {
                    currentRecord = nextRecord;
                    if(currentRecord == null){
                        throw new NoSuchElementException("Record doesn't exists");
                    }
                    nextRecord = getNextRecord();
                    return currentRecord;
                }

                @Override
                public void remove() {
                    // Not required
                }
            };
            return _iter;

        } catch (FileNotFoundException e) {
            // TODO LOG statement
            e.printStackTrace();
        }

        return null;
    }

    public static ClaimsRecord processRecord(String line){
        //TODO code to process line and create Record object.
        return null;
    }

}
